package system1

import (
	"fmt"
	"net/http"
	"strconv"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system1"
	system1Req "github.com/flipped-aurora/gin-vue-admin/server/model/system1/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type WorkflowResponse struct {
	Code int    `json:"code"`
	Data string `json:"data"`
	Msg  string `json:"msg"`
}

type WorkflowDefApi struct {
}

var WorkflowDefService = service.ServiceGroupApp.System1ServiceGroup.WorkflowDefService

// CreateWorkflowDef 创建审批流
// @Tags WorkflowDef
// @Summary 创建审批流
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.WorkflowDef true "创建审批流"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /WorkflowDef/createWorkflowDef [post]
func (WorkflowDefApi *WorkflowDefApi) CreateWorkflowDef(c *gin.Context) {
	var WorkflowDef system1.WorkflowDef
	err := c.ShouldBindJSON(&WorkflowDef)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	WorkflowDef.CreatedBy = utils.GetUserID(c)

	if err := WorkflowDefService.CreateWorkflowDef(&WorkflowDef); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteWorkflowDef 删除审批流
// @Tags WorkflowDef
// @Summary 删除审批流
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.WorkflowDef true "删除审批流"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /WorkflowDef/deleteWorkflowDef [delete]
func (WorkflowDefApi *WorkflowDefApi) DeleteWorkflowDef(c *gin.Context) {
	ID := c.Query("ID")
	userID := utils.GetUserID(c)
	if err := WorkflowDefService.DeleteWorkflowDef(ID, userID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteWorkflowDefByIds 批量删除审批流
// @Tags WorkflowDef
// @Summary 批量删除审批流
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /WorkflowDef/deleteWorkflowDefByIds [delete]
func (WorkflowDefApi *WorkflowDefApi) DeleteWorkflowDefByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	userID := utils.GetUserID(c)
	if err := WorkflowDefService.DeleteWorkflowDefByIds(IDs, userID); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateWorkflowDef 更新审批流
// @Tags WorkflowDef
// @Summary 更新审批流
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.WorkflowDef true "更新审批流"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /WorkflowDef/updateWorkflowDef [put]
func (WorkflowDefApi *WorkflowDefApi) UpdateWorkflowDef(c *gin.Context) {
	var WorkflowDef system1.WorkflowDef
	err := c.ShouldBindJSON(&WorkflowDef)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	WorkflowDef.UpdatedBy = utils.GetUserID(c)

	if err := WorkflowDefService.UpdateWorkflowDef(WorkflowDef); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindWorkflowDef 用id查询审批流
// @Tags WorkflowDef
// @Summary 用id查询审批流
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1.WorkflowDef true "用id查询审批流"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /WorkflowDef/findWorkflowDef [get]
func (WorkflowDefApi *WorkflowDefApi) FindWorkflowDef(c *gin.Context) {
	ID := c.Query("ID")
	if reWorkflow, err := WorkflowDefService.GetWorkflowDef(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reWorkflow": reWorkflow}, c)
	}
}

// GetWorkflowDefList 分页获取审批流列表
// @Tags WorkflowDef
// @Summary 分页获取审批流列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1Req.WorkflowDefSearch true "分页获取审批流列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /WorkflowDef/getWorkflowDefList [get]
func (WorkflowDefApi *WorkflowDefApi) GetWorkflowDefList(c *gin.Context) {
	var pageInfo system1Req.WorkflowDefSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := WorkflowDefService.GetWorkflowDefInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetWorkflowDef 用id查询审批流Def
// @Tags WorkflowDef
// @Summary 用id查询审批流Def
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1.WorkflowDef true "用id查询审批流Def"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /WorkflowDef/getWorkflowDef [get]
func (WorkflowDefApi *WorkflowDefApi) GetWorkflowDef(c *gin.Context) {
	ID := c.Query("ID")
	if DbWorkflowDef, err := WorkflowDefService.GetWorkflowDef(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		/*data := make(map[string]interface{})
		 err := json.Unmarshal([]byte(reWorkflow.Flowdata), &data)
		 if err != nil {
			 response.FailWithMessage("查询失败", c)
		 }
		 data["test"] = "asdfasdf"
		 response.OkWithData(gin.H{"data": data}, c)
		*/
		WorkflowDef := "{ \"code\": \"0\", \"msg\": \"success\", \"data\": " + string(DbWorkflowDef.Flowdata) + "}"
		c.String(http.StatusOK, WorkflowDef)
	}
}

// UpdateWorkflowDefByChart 通过图形用id修改审批流Def
// @Tags WorkflowDef
// @Summary 用id修改审批流Def
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1.WorkflowDef true "通过图形用id修改审批流Def"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /WorkflowDef/updateWorkflowDefByChart [post]
func (WorkflowDefApi *WorkflowDefApi) UpdateWorkflowDefByChart(c *gin.Context) {
	var WorkflowDef system1.WorkflowDef
	idStr := c.Query("ID")
	jsonData, _ := c.GetRawData()
	//fmt.Println("jsonData", string(jsonData))
	//var m map[string]interface{}
	//jsonObj, _ := simplejson.NewJson(jsonData)
	//fmt.Println(jsonObj)
	ID, _ := strconv.Atoi(idStr)
	WorkflowDef.ID = uint(ID) //uint(jsonObj.Get("tableId").MustInt(0))
	fmt.Println("WorkflowDef.ID", WorkflowDef.ID)

	strId := strconv.Itoa(int(WorkflowDef.ID))
	WorkflowDef, _ = WorkflowDefService.GetWorkflowDef(strId)
	WorkflowDef.UpdatedBy = utils.GetUserID(c)
	WorkflowDef.Flowdata = string(jsonData)
	fmt.Println("WorkflowDef.ID", WorkflowDef)

	if err := WorkflowDefService.UpdateWorkflowDef(WorkflowDef); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {

		response.OkWithMessage("更新成功", c)
	}

}
